# 明确使用 bullseye 基础镜像
FROM php:7.4-fpm-bullseye

# 清理并设置阿里云源
RUN rm -f /etc/apt/sources.list && \
    rm -rf /etc/apt/sources.list.d/* && \
    echo "deb http://mirrors.aliyun.com/debian bullseye main contrib non-free" > /etc/apt/sources.list && \
    echo "deb http://mirrors.aliyun.com/debian-security bullseye-security main contrib non-free" >> /etc/apt/sources.list && \
    echo "deb http://mirrors.aliyun.com/debian bullseye-updates main contrib non-free" >> /etc/apt/sources.list

# 安装依赖（移除 libonig-dev）
RUN apt-get update && apt-get install -y --no-install-recommends \
    libfreetype6-dev \
    libjpeg62-turbo-dev \
    libpng-dev \
    libzip-dev \
    zip \
    unzip \
    git \
    gcc \
    g++ \
    make \
    autoconf \
    libc-dev \
    pkg-config \
    && docker-php-ext-configure gd --with-freetype --with-jpeg \
    && docker-php-ext-install -j$(nproc) gd pdo_mysql mysqli zip bcmath fileinfo opcache \
    && apt-get clean && rm -rf /var/lib/apt/lists/*

# 安装 PHP Redis/MySQL 扩展包（按操作系统类型选择包管理器）
# - Debian/Ubuntu: apt-get install -y php-redis php-mysql
# - RHEL/CentOS:   yum install -y php-redis php-mysql
# 注意：官方 php 镜像推荐使用 pecl/docker-php-ext-* 管理扩展，
# 这里按需求提供系统包安装作为兼容补充，若包不可用则忽略但不失败。
RUN set -eux; \
    if command -v apt-get >/dev/null 2>&1; then \
        apt-get update && apt-get install -y --no-install-recommends php-redis php-mysql || true; \
    elif command -v yum >/dev/null 2>&1; then \
        yum install -y php-redis php-mysql || true; \
    fi

# 安装并启用 Redis 扩展（通过 pecl，确保在官方 php 镜像下可用）
RUN set -eux; \
    apt-get update && apt-get install -y --no-install-recommends $PHPIZE_DEPS && \
    pecl install redis-5.3.7 && \
    docker-php-ext-enable redis mysqli && \
    apt-get clean && rm -rf /var/lib/apt/lists/*

# 安装 Composer
COPY --from=composer:2.8.8 /usr/bin/composer /usr/local/bin/composer
RUN composer config -g repos.packagist composer https://mirrors.aliyun.com/composer/

WORKDIR /app
RUN mv "$PHP_INI_DIR/php.ini-production" "$PHP_INI_DIR/php.ini" \
    && echo 'memory_limit = 256M' >> "$PHP_INI_DIR/php.ini"